package com.tigger.core.controller;

import com.tigger.common.utils.WebParameter;
import com.tigger.core.po.EsFilePo;
import com.tigger.core.redis.RedisServer;
import com.tigger.core.service.IEsFileBiz;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@RestController
public class EsFileController {

    // 从 application.properties.bak 中读取配置，如取不到默认值为Hello World
    //@Value("${application.hell: Hello World}")
    private String hello = "Hello World";
    @Autowired
    private IEsFileBiz esFileBiz;
    @Autowired
    private RedisServer redisServer;

    @RequestMapping("/file")
    public String index(Map<String, Object> model, HttpServletRequest request) {
        esFileBiz.findById("7ada6fa49ce941c687103bc705a0c559");
//        List<EsFilePo> fileList = redisServer.getList("fileList", EsFilePo.class);
//        System.out.println(fileList.get(0).getFileId());
        return "index";
    }

    @RequestMapping("/page")
    public ModelAndView page() {
        Map<String, Object> modelMap = new ConcurrentHashMap<>();
        ModelAndView mav = new ModelAndView("page/page");
        mav.addObject("content", hello);
        mav.addAllObjects(modelMap);
        return mav;
    }

    /*@RequestMapping("/exportFileXls")
    public void exportXls(HttpServletRequest request, HttpServletResponse response, ModelMap map) throws Exception {
        Map<String, Object> params = WebParameter.getParamsMap(request);
        List<EsFilePo> files = esFileBiz.findByPage(params);
        List<EsFileEntity> entities = copyCollection(files);

        ExportParams exportParams = new ExportParams("系统文件信息", "导出人: asfaloth", "导出信息");
        exportParams.setType(ExcelType.XSSF);
//		exportParams.setAddIndex(true);
//		exportParams.setIndexName("序号");
        map.put(NormalExcelConstants.FILE_NAME, "文件信息");
        map.put(NormalExcelConstants.CLASS, EsFileEntity.class);
        map.put(NormalExcelConstants.PARAMS, exportParams);
        map.put(NormalExcelConstants.DATA_LIST, entities);
        PoiBaseView.render(map, request, response, NormalExcelConstants.EASYPOI_EXCEL_VIEW);
    }

    private List<EsFileEntity> copyCollection(List<EsFilePo> files) {
        List<EsFileEntity> entities = new ArrayList<>();
        for (EsFilePo file : files) {
            if (files != null) {
                EsFileEntity entity = new EsFileEntity();
                BeanUtils.copyProperties(file, entity);
                entities.add(entity);
            }
        }
        return entities;
    }*/

}
